﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
using WebApplication1.Models.Database;
using System.Collections;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        private readonly newsContext _db;
        public HomeController(newsContext db)
        {
            _db = db;
        }


        [Route("/index.html")]
        [Route("/")]
        [Route("{controller}/{action}.html")]
        public IActionResult Index(int cid = 0, int page=1)
        {
            
            var list=_db.Classifications.ToList();
            var news=_db.News.ToList();
            var pagemax = 0;
            if (news.Count % 6 == 0)
            {
                pagemax=news.Count/6;
            }
            else
            {
                pagemax= (news.Count/6)+1;
            }

            ViewData["page"] = page;
            if (cid > 0)
            {
                news=news.Where(x => x.Classid == cid).ToList();
            }


            ViewData["classification"] = list;
            ViewData["news"] = news;
            ViewData["cid"]=cid;
            ViewData["pagemax"] = pagemax;
            return View();
        }

        [Route("/detail.html")]
        [Route("{controller}/{action}.html")]
        public IActionResult Detail(int id)
        {
            var list = _db.Classifications.ToList();
            ViewData["classification"] = list;
            var news=_db.News.FirstOrDefault(x=>x.Id == id);
            ViewData["news"] = news;
            return View();
        }

        [Route("{controller}/{action}.html")]
        [Route("{controller}/{action}")]
        [Route("{action}.html")]
        [Route("{action}")]
        public IActionResult Search(string text)

        {   var list=_db.News.Where(x=>x.Title.Contains(text)||x.Content.Contains(text)).ToList();
            ViewData["list"] = list;
            var list1 = _db.Classifications.ToList();
            ViewData["classification"] = list1;
            return View();
        }

    }
}
